package com.zhengsr.latte.ui.recycler;

import com.zhengsr.latte.ui.recycler.adapter.RMultiBean;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.LinkedHashMap;

/**
 * Created by zhengshaorui on 2018/5/27.
 */

public class MultipleBean implements RMultiBean {
    //用一个queue，防止数据过大崩溃,用final就只是被初始化一次，这样就可以保存啦
    public final ReferenceQueue<LinkedHashMap<Object,Object>> ITEM_QUEUE = new ReferenceQueue<>();
    public final   LinkedHashMap<Object,Object> MULTIPLE_FIELDS = new LinkedHashMap<>();
    //用软引用
    public final  SoftReference<LinkedHashMap<Object,Object>> FIELDS_REFERENCE = new
            SoftReference<>(MULTIPLE_FIELDS,ITEM_QUEUE);

    public MultipleBean(MultipleBuilder.Builder builder) {
        FIELDS_REFERENCE.get().putAll(builder.linkedHashMap);
    }

    public LinkedHashMap<?,?> getFiled(){
        return FIELDS_REFERENCE.get();
    }

    public <T> T getItemField(Object key){
        return (T) FIELDS_REFERENCE.get().get(key);
    }

    @Override
    public int getItemType() {
        return (int) FIELDS_REFERENCE.get().get(MultipleConfig.ITEM_TYPE);
    }

    @Override
    public int getSpanSize() {
        return (int) FIELDS_REFERENCE.get().get(MultipleConfig.SPAN_SIZE);
    }
}
